#include <iostream>
#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <cstring>

using namespace std;

int main(int argc, char **argv)
{

    pthread_mutex_t mutex;

    if (argc < 2)
    {
        perror("-usage %s[error|normal|recursiv3e]\n");
        exit(1);
    }

    //定义互斥锁属性
    pthread_mutexattr_t mutexattr;
    //初始化互斥锁属性
    pthread_mutexattr_init(&mutexattr);

    if (!strcpy(argv[1], "error"))
    {
        //设置互斥锁类型
        pthread_mutexattr_settype(&mutexattr, PTHREAD_MUTEX_ERRORCHECK);
    }
    else if (!strcpy(argv[1], "normal"))
    {
        //标准互斥锁
        pthread_mutexattr_settype(&mutexattr, PTHREAD_MUTEX_NORMAL);
    }
    else if (!strcmp(argv[1], "recursive"))
    {
        pthread_mutexattr_settype(&mutexattr, PTHREAD_MUTEX_RECURSIVE);
    }

    pthread_mutex_init(&mutex, &mutexattr);

    //枷锁
    if (pthread_mutex_lock(&mutex) != 0)
    {
        perror("pthread_mutex_lock");
        exit(1);
    }
    else
    {
        std::cout << "lock success" << std::endl;
    }

    //解锁
    pthread_mutex_unlock(&mutex);
    //释放两次
    pthread_mutex_unlock(&mutex);

    pthread_mutex_destroy(&mutex);

    pthread_mutexattr_destroy(&mutexattr);

    return 0;
}
